/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game;

import org.apache.log4j.Logger;
import game.chips.Chip;

public class Step {
    private static final Logger LOG=Logger.getLogger("logfile");
    
    private final String moved = " turn from";
    private final String canceled = " cant move from";
    private final String kill = " kill";
    private final String edge = " cant move through the edge";

    private final Player first;
    private final Player second;

    private String result = null;
    private String action = moved;

    public Step(Player first, Player second) {
        this.first = first;
        this.second = second;
    }

    public void doStep() {
        game.chips.Chip tmp = null;

        for (game.chips.Chip walk : first.getChips()) {
            for (game.chips.Chip wait : second.getChips()) {
                if (walk.getAfterTurn().get(0) == wait.getBeforeTurn().get(0) && walk.wasMoved()) {

                    if (walk.getAfterTurn().get(1) < 0 || walk.getAfterTurn().get(1) > Game.getSize() - 1) {
                        abroadChip(walk);

                    } else if (walk.getAfterTurn().get(1) == wait.getBeforeTurn().get(1)) {
                        notMovedChip(walk);

                    } else if (walk.getAfterTurn().get(1) > wait.getBeforeTurn().get(1)) {
                        if (first.getNumPlayer() == 0)
                            tmp = killChip(walk, wait);
                        else
                            movedChip(walk, wait);

                    } else if (walk.getAfterTurn().get(1) < wait.getBeforeTurn().get(1)) {
                        if (first.getNumPlayer() == 0)
                            movedChip(walk, wait);
                        else
                            tmp = killChip(walk, wait);
                    }
                }
            }
        }
        first.getGame().getWriter().print(first.getGame() + result);
        LOG.warn(first.getGame() + result);
        if(action.equals(kill)) {
            tmp.setOut();
            second.chipKilled();
        }
    }

    public boolean isLastStep() {
        if (second.getCountChips() == 0 || (first.getCountChips() + second.getCountChips()) == Game.getSize()) {
            //System.out.println((first.getCountChips() > second.getCountChips() ? first : second) + " win");
            return true;
        } else
            return false;
    }

    private game.chips.Chip killChip(game.chips.Chip walk, Chip wait) {
        if(!action.equals(canceled) && !action.equals(edge) && wait.isFirst()) {
            result = walk.toString() + kill + walk.stepToString();
            action = kill;
        }
        return wait;
    }

    private void movedChip(game.chips.Chip walk, Chip wait) {
        if(!action.equals(canceled) && !action.equals(edge) && wait.isFirst())
            result = walk.toString() + moved + walk.stepToString();
    }

    private void notMovedChip(game.chips.Chip walk) {
        result = walk.toString() + canceled + walk.stepToString();
        action = canceled;
        walk.setAfterTurn(walk.getBeforeTurn());
    }

    private void abroadChip(game.chips.Chip walk) {
        result = walk.toString() + edge + walk.stepToString();
        action = edge;
        walk.setAfterTurn(walk.getBeforeTurn());
    }
}
